<template>
    <section class="form-container">
        <el-form :inline="true" :model="filter" size="mini" class="form-filter">
            <el-form-item label="用户名">
                <el-input v-model="filter.username" placeholder="请输入条件查询" @change="getPage()"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="getPage()" icon="el-icon-search">查询</el-button>
                <el-button @click="showUpdate({})" icon="el-icon-plus">添加</el-button>
            </el-form-item>
        </el-form>
        <div class="form-content">
            <el-table
                v-loading="loading"
                style="width: 100%"
                :data="page.records"
                border
                stripe
                :header-cell-style = "headerCellStyle"
            >
                <el-table-column prop="id" label="ID" width="60"/>
                <el-table-column prop="username" label="登录名"/>

                <el-table-column prop="roleName" label="角色"/>
                <el-table-column prop="mobile" label="手机号"/>
                <el-table-column prop="email" label="邮箱"/>
                <el-table-column label="类型" width="110px">
                    <template #default="scope">
                        <el-tag :type="scope.row.userTypeStyle">
                            {{ scope.row.userTypeName }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="remark" label="备注"/>

                <el-table-column label="状态" width="80px">
                    <template #default="scope">
                        <el-switch
                            v-model="scope.row.status"
                            :disabled="scope.row.userType==1"
                            @change="statusSwitch(scope.row.status,scope.row.id)"
                        />
                    </template>
                </el-table-column>
                <el-table-column prop="updateName" label="更新人"/>
                <el-table-column prop="updateTime" label="更新时间"/>

                <el-table-column fixed="right" label="操作" width="250">
                    <template #default="scope">
                        <div v-if="scope.row.userType != 1">
                            <el-button size="mini" style="margin:0 0px 10px 10px;" @click="showUpdate(scope.row)">编辑
                            </el-button>
                            <el-popconfirm title="确定删除吗？" @onConfirm="handleDelete(scope.row.id)">
                                <template #reference>
                                    <el-button slot="reference" size="mini" style="margin:0 0px 10px 10px;">删除
                                    </el-button>
                                </template>
                            </el-popconfirm>

                            <el-popconfirm title="确定重置密码吗？" @onConfirm="restPassword(scope.row.id)">
                                <template #reference>
                                    <el-button slot="reference" size="mini" style="margin:0 0px 10px 10px;">重置密码
                                    </el-button>
                                </template>
                            </el-popconfirm>
                        </div>
                    </template>
                </el-table-column>
            </el-table>

            <el-pagination
                background
                :current-page="page.current"
                :page-sizes="[10, 20, 30, 50,100]"
                :page-size="page.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="page.total"
                style="margin-top:20px;"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
            />

            <Update ref="updateRef" @refresh="getPage()"/>
        </div>
    </section>
</template>
<script>
import {AdminApi} from '@/api/system/admin'
import Update from './Update.vue'

export default {
    name: 'SystemAdminList',
    components: {
        Update
    },
    data() {
        return {
            page: {
                total: 0,
                size: 10,
                current: 1,
                pages: 0,
                records: []
            },
            filter: {
                pageIndex: 0,
                pageSize: 0,
                username: ''
            },
            loading: false,
            headerCellStyle:{
                "background-color":"#F2F5FC"
            }
        }
    },
    created() {
        this.getPage()
    },
    methods: {
        getPage() {
            const that = this
            this.loading = true
            this.filter.pageIndex = this.page.current
            this.filter.pageSize = this.page.size
            AdminApi.getPage(this.filter)
                .then((response) => {
                    const dataList = []
                    response.records.forEach(function (item) {
                        const roleIds = []
                        let roleName = ''
                        item.roleList.forEach(function (roleItem) {
                            roleIds.push(roleItem.id)
                            roleName += roleItem.roleName + '|'
                        })

                        item['roleIds'] = roleIds
                        item['roleName'] = roleName.substring(0, roleName.length - 1)

                        dataList.push(item)
                    })

                    response.records = dataList

                    that.page = response
                    that.loading = false
                }).catch((err) => {
                that.loading = false
            })
        },

        statusSwitch(val, id) {
            const params = {
                id: id,
                status: val
            }

            AdminApi.updateStatus(params).then((response) => {
            })
        },

        // 删除数据
        handleDelete(id) {
            AdminApi.delete(id)
                .then((response) => {
                    this.getPage()
                })
        },

        // 重置密码
        restPassword(id) {
            AdminApi.restPassword(id)
                .then((response) => {
                    this.$alert('密码只显示一次，无法找回，请保存。<br/>新密码：<b style="color:#F00;">' + response + '</b>', '重置成功', {
                        confirmButtonText: '知道了',
                        dangerouslyUseHTMLString: true,
                        callback: () => {
                        }
                    })
                })
        },

        handleSizeChange(val) {
            this.page.size = val
            this.page.current = 1
            this.getPage()
        },

        showUpdate(data) {
            this.$refs.updateRef.showUpdate(data)
        },

        handleCurrentChange(val) {
            this.page.current = val
            this.getPage()
        }

    }
}

</script>
